import request from 'umi-request';

export default {
  namespace: 'customers',

  state: [
    {
      key: '1',
      firstName: 'John',
      lastName: 'Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
      tags: ['nice', 'developer'],
    },
    {
      key: '2',
      firstName: 'Jim',
      lastName: 'Green',
      age: 42,
      address: 'London No. 1 Lake Park',
      tags: ['loser'],
    },
    {
      key: '3',
      firstName: 'Joe',
      lastName: 'Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
      tags: ['cool', 'teacher'],
    },
  ],

  reducers: {
    info(_, { payload }) {
      const { employees } = payload;
      return employees;
    },

    add(state,{payload}){
      console.log('添加成功');
      const { e: newCustomer } = payload;
      newCustomer.tags = []
      let temp = state;
      temp.push(newCustomer);
      return temp;

    },
    update(state, { payload }) {
      const { e: newCustomer } = payload;

      let index = state.findIndex((item) => item.key === newCustomer.key);
      let temp = state;
      temp[index] = newCustomer;
      console.log(temp);
      return temp;
    },

    deleteTable(state, { payload }) {
      const { key: targetId } = payload;

      return state.filter((t) => t.key !== targetId);
    },
  },

    effects: {
      *getAll(_, { call, put }) {
        const response = yield call(request.get, '/api/customers');
        console.log(response);
        yield put({
          type: 'info',
          payload: {
            employees: response,
          },
        });
      },
  
      *updateValue(_, { call, select }) {
        let value = yield select((store) => store.customers);
        console.log(value);
        yield call(request.post, '/api/customers', { data: value });
      },
    },
};
